package com.dubbo.util;

import org.I0Itec.zkclient.ZkClient;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
@Component
@Lazy
public class ZookeeperUtil {
    private static String ROOT = "/dubbo";

    private static String zkHost;

    public static void setZkHost(String zkHost) {
        ZookeeperUtil.zkHost = zkHost;
    }

    private final static Integer TIMEOUT = 3000;

    private Map<String, Set<String>> serviceMethod=new ConcurrentHashMap<>();

    private Map<String, Set<String>> serviceAddress=new ConcurrentHashMap<>();

    public Map<String, Set<String>> getServiceMethod() {
        return serviceMethod;
    }

    public Map<String, Set<String>> getServiceAddress() {
        return serviceAddress;
    }

    public Set<String> getRpcInterfaces(){
        return serviceMethod.keySet();
    }
    public Set<String> getRpcMethods(String interfaceName){
        return serviceMethod.get(interfaceName);
    }
    public Set<String> getRpcAddress(String interfaceName){
        return serviceAddress.get(interfaceName);
    }

    @PostConstruct
    public void init(){
        new Thread(()->{
            List<String> services=getServiceNameList();
            for(String service:services){
                Set<String> methodsSet=serviceMethod.get(service);
                Set<String> directAddress=serviceAddress.get(service);
                if(methodsSet==null){
                    methodsSet=new HashSet<>();
                }
                if(directAddress==null){
                    directAddress=new HashSet<>();
                }
                List<String> providers=getServices(ROOT+"/"+service+"/providers");
                for(String provider:providers){
                    if(provider!=null&&!"".equals(provider)) {
                        String providerDecoder = URLDecoderString(provider);
                        String dubbo = "dubbo://"+CRequest.UrlPage(providerDecoder).split("//")[1].split("/")[0];
                        Map<String, String> dubboParam = CRequest.URLRequest(providerDecoder);
                        String methods = dubboParam.get("methods");
                        methodsSet.addAll(Arrays.asList(methods.split(",")));
                        directAddress.add(dubbo);
                        serviceMethod.put(service, methodsSet);
                        serviceAddress.put(service, directAddress);
                    }
                }
            }
        }).start();
    }
    public  List<String> getServiceNameList() {
        return getServices(ROOT);
    }



    public   List<String> getServices(String address) {
        ZkClient zkClient = new ZkClient(zkHost, TIMEOUT);
        List<String> zkHostList = zkClient.getChildren(address);
        zkClient.close();
        return zkHostList;
    }
    public static String URLDecoderString(String str) {
        String result = "";
        if (null == str) {
            return "";
        }
        try {
            result = java.net.URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

}
